home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 11 / FM Towns Free Software Collection 11.iso / t_os / lib / yotpin / src / relief.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-22  |  7.2 KB  |  396 lines

  1. /*
  2. *    Yamana's Otomeza Plug-in Tool
  3. *        レリーフもどき
  4. *    
  5. *    1995.07.13    乙女座プラグイン対応版
  6. *    1995.08.13    3倍速化、パレット無変更モード追加、品質向上
  7. *    1995.08.20    うっすらモード追加
  8. *    
  9. */
  10. #include    "otome_pi.h"
  11.  
  12. //#define    DEBUG
  13.  
  14. const char longname[]  = "EFFECT: レリーフ";
  15. int            cnfg_max = 2;
  16. PI_CNFG        cnfg[] =
  17.             {    /* 1234567890123456 ,min,max,def,set */
  18.                 { "動作モード"        ,  0,  2,  0,  0 },
  19.                 { "良 ←  感度     ",  0, 14,  4,  4 },
  20.             };
  21.  
  22. /* 使わないとき 0 にする */
  23. #define        USE_ENV     PI_SET_ENV
  24. #define        USE_TYPE    PI_EFFC_ALORSL
  25. #include    "otome_pi.c"
  26.  
  27.  
  28. #define    PASTEL_LEVEL    128
  29.  
  30. /*************** パレット操作部 ****************/
  31.  
  32. int     pal[256];
  33.  
  34. void getPalette(col)
  35. int col;
  36. {
  37.     int     i,j;
  38.     char    *p = pi_imge->clut ;
  39.     
  40.     p += 4;
  41. #if 0
  42.     for( i=0; i<col ; i++)
  43.         printf("[%2d]=%2d %2x,%2x,%2x\r\n"
  44.                     ,i,p[i*8],p[i*8+4],p[i*8+5],p[i*8+6] );
  45. #endif
  46.     
  47.     for( i=0 ; i<col ; i++ )
  48.     {
  49.         j = i*8;
  50.         /* 輝度計算 */
  51.         pal[ p[j] & 0xff ] =
  52.             (      ( p[j+4] & 0xff )* 11        /* B */
  53.                 + ( p[j+5] & 0xff )* 30        /* R */
  54.                 + ( p[j+6] & 0xff )* 59        /* G */
  55.             )>>10 ;
  56.         
  57.         /* 25500~0 >> 24~0 までの値が入る. */
  58.     }
  59.     
  60. }
  61.  
  62. #define    C_GRAY    0x07
  63. #define    C_WHITE    0x0f
  64. #define    C_BLACK    0x00
  65. #define    GRAY32K     0x3def    /* 各下位 4ビットを 1 とした */
  66. #define    WHITE32K    0x7fff
  67. #define    BLACK32K    0x0000
  68.  
  69. void setPalette( mode )
  70. int     mode;
  71. {
  72.     char    *p;
  73.     int     i,j,col;
  74.     
  75.     static char pal_data[][4]=
  76.         {
  77.             {    C_BLACK,0x00,0x00,0x00    },
  78.             {    C_GRAY ,0x70,0x70,0x70    },
  79.             {    C_WHITE,0xf0,0xf0,0xf0    }
  80.         };
  81.     
  82.     if( mode>1 )    return;
  83.     if( mode>0 )
  84.     {
  85.         col = (PI_PIX == 8 ? 256:16);
  86.         p=pi_imge->clut+8;
  87.         {    /* 黒 */
  88.             *(p  ) = *(p  )/2 ;
  89.             *(p+1) = *(p+1)/2 ;
  90.             *(p+2) = *(p+2)/2 ;
  91.         }
  92.         
  93.         for( p+=8,i=1 ; i<15 ; i++,p+=8 )
  94.         {    
  95.             *(p  ) = (*(p  ) + 0x70 )/2;
  96.             *(p+1) = (*(p+1) + 0x70 )/2;
  97.             *(p+2) = (*(p+2) + 0x70 )/2;
  98.         }
  99.         {    /* 白 */
  100.             *(p  ) = (*(p  ) + 0xff )/2;
  101.             *(p+1) = (*(p+1) + 0xff )/2;
  102.             *(p+2) = (*(p+2) + 0xff )/2;
  103.         }
  104.         for( p+=8 ; i<col ; i++,p+=8 )
  105.         {    
  106.             *(p  ) = (*(p  ) + 0x70 )/2;
  107.             *(p+1) = (*(p+1) + 0x70 )/2;
  108.             *(p+2) = (*(p+2) + 0x70 )/2;
  109.         }
  110.         
  111.     }
  112.     else    /* mode==0 */
  113.     {
  114.         for( i=0, p=pi_imge->clut+4 ; i<3 ;i++ )
  115.         {
  116.             j = 8*(pal_data[i][0]) ;
  117.             
  118.             p[j+4] = pal_data[i][1] ;
  119.             p[j+5] = pal_data[i][2] ;
  120.             p[j+6] = pal_data[i][3] ;
  121.         }
  122.     }
  123.     
  124. }
  125.  
  126. /***************** レリーフ処理部 *******************/
  127.  
  128. int     width,height;
  129. char    *buf[2];
  130.  
  131. /* こんなのでもそこそこの処理速度で十分それらしく見える */
  132.  
  133. int palcmp(x)
  134. int x;
  135. {
  136.     return ( pal[ buf[0][x] ]*2 - pal[ buf[1][x+1] ] - pal[ buf[0][x+1] ] );
  137. }
  138.  
  139. int rgbcmp(x)
  140. int x;
  141. {
  142.     return (buf[0][x]*2 - buf[1][x+1] - buf[0][x+1] );
  143. }
  144.  
  145. /*********************************/
  146.  
  147. int getPix16(_x,_y)
  148. int _x,_y;
  149. {
  150.     return ((pi_imge->image[ ((_x)>>1) + (_y)*(pi_imge->size.x>>1) ]
  151.                     >>(((_x) & 1)? 4:0) )& 0x0f );
  152. }
  153. int getPix256(_x,_y)
  154. int     _x,_y;
  155. {
  156.     return (pi_imge->image[ (_x) + (_y)*(pi_imge->size.x) ])& 0xff ;
  157. }
  158. int getPix32k(_x,_y)
  159. int _x,_y;
  160. {
  161.     return WORD( pi_imge->image + ((_x)<<1) + (_y)*(pi_imge->size.x<<1) );
  162. }
  163.  
  164.  
  165.  
  166. void    putPix16(_x,_y,_c)
  167. int     _x,_y,_c;
  168. {    
  169.     char    *p;
  170.     
  171.     p = &pi_imge->image[ ((_x)>>1) + (_y)*(pi_imge->size.x>>1) ] ;
  172.     
  173.     if( (_x) & 1 )    *p = (*p & 0x0f)|(((_c)& 0x0f)<<4) ;
  174.             else    *p = (*p & 0xf0)| ((_c)& 0x0f) ;
  175.     
  176. }
  177. void    putPix256(_x,_y,_c)
  178. int     _x,_y,_c;
  179. {
  180.     pi_imge->image[ (_x) + (_y)*(pi_imge->size.x) ] = ((_c)& 0xff);
  181. }
  182.  
  183. void    putPix32k(_x,_y,_c)
  184. int     _x,_y,_c;
  185. {
  186.     WORD( pi_imge->image + (((_x)<<1) + (_y)*(pi_imge->size.x<<1)) )
  187.         = (_c) ;
  188. }
  189.  
  190. /*********************************/
  191.  
  192.  
  193. void getBuf(fr,y,n, getFunc)
  194. FRAME    *fr;
  195. int     y,n;
  196. int     (*getFunc)();
  197. {
  198.     int     x;
  199.     
  200.     for(x=0; x<width; x++)
  201.         buf[n][x] = getFunc( (fr->lupx + x),(fr->lupy + y) );
  202.     buf[n][x] = buf[n][x-1];
  203.     
  204. }
  205.  
  206. void getBuf32K(fr,y,n, getFunc )
  207. FRAME    *fr;
  208. int     y,n;
  209. int     (*getFunc)();
  210. {
  211.     int     x,col;
  212.     
  213.     for(x=0; x<width+1; x++)
  214.     {
  215.         col = getFunc( (fr->lupx + x), (fr->lupy + y) );
  216.         buf[n][x] = 
  217.         (      (((col)    ) & 0x1f)*11
  218.             + (((col)>>5 ) & 0x1f)*30
  219.             + (((col)>>10) & 0x1f)*59
  220.         )>>7 ;
  221.         
  222.         /* 3100-0 >> 24-0 */
  223.     }
  224. }
  225.  
  226. /*********************************/
  227.  
  228. void shiftBuf()
  229. {
  230.     char     *t = buf[0];
  231.     
  232.     buf[0] = buf[1];
  233.     buf[1] = t ;
  234. }
  235.  
  236. void    nullFunc(fr,sy,col ,putFunc )
  237. FRAME    *fr;
  238. int     sy,col;
  239. void    (*putFunc)();
  240. {    
  241. }
  242.  
  243. void    grayLine(fr,sy ,putFunc )
  244. FRAME    *fr;
  245. int     sy;
  246. void    (*putFunc)();
  247. {
  248.     FRAME    para;
  249.     
  250.     para.lupx = fr->lupx;
  251.     para.rdwx = fr->rdwx;
  252.     para.lupy = para.rdwy = (fr->lupy + sy);
  253.     
  254.     EGB_rectangle( EgbPtr, ¶);
  255. }
  256.  
  257. void Relieve(fr,y,depth, putFunc, cmpFunc )
  258. FRAME    *fr;
  259. int     y,depth;
  260. void    (*putFunc)();
  261. int     (*cmpFunc)();
  262. {
  263.     int     x,col,tmp;
  264.     
  265.     for( x=0; x<width ; x++)
  266.     {
  267.         tmp = cmpFunc( x );
  268.         
  269.              if( tmp== 0      )    continue;
  270.         else if( tmp<(-depth) )    col = WHITE32K;
  271.         else if( tmp>  depth  )    col = BLACK32K;
  272.         else continue;
  273.         
  274.         putFunc( (fr->lupx + x), (fr->lupy + y), col );
  275.     }
  276. }
  277.  
  278. /*************/
  279.  
  280. int mk_Relief( fr, depth, pmode, bufFunc, getFunc, putFunc, cmpFunc )
  281. FRAME    *fr;
  282. int     depth,pmode;
  283. void     (*bufFunc)();
  284. int     (*getFunc)();
  285. void    (*putFunc)();
  286. int     (*cmpFunc)();
  287. {
  288.     int     y;
  289.     int     ch,mx,my;
  290.     void    (*grayFunc)();
  291.     
  292.     if( pmode == 0 )    grayFunc = grayLine;
  293.                 else    grayFunc = nullFunc;
  294.     
  295.     bufFunc( fr, 0, 0, getFunc );
  296.     for( y=0 ; y<height-1 ; y++ )
  297.     {
  298.         MOS_rdpos(&ch,&mx,&my);
  299.         if( (ch & 3)==2 ) break;
  300.         
  301.         bufFunc ( fr, y+1, 1    , getFunc );
  302.         grayFunc( fr, y  ,        putFunc );
  303.         Relieve ( fr, y  , depth, putFunc, cmpFunc );
  304.         shiftBuf();
  305.     }
  306.     memcpy( buf[1], buf[0], width+1 );
  307.     grayFunc( fr, y  ,        putFunc );
  308.     Relieve ( fr, y  , depth, putFunc, cmpFunc );
  309.     
  310.     
  311.     return NOERR;
  312. }
  313.  
  314. /***************************************/
  315.  
  316. int APL_exec()
  317. {
  318.     int     depth,pmode;
  319.     char    *mem;
  320.     FRAME    fr;
  321.     
  322. #ifdef DEBUG
  323.     clock_t    t;
  324.     t = clock();
  325. #endif
  326.     
  327.     pmode = cnfg[0].val;
  328.     depth = cnfg[1].val;
  329.     
  330.     fr.lupx = WORD( g_para + 0 );
  331.     fr.lupy = WORD( g_para + 2 );
  332.     fr.rdwx = WORD( g_para + 4 );
  333.     fr.rdwy = WORD( g_para + 6 );
  334.     
  335.     /* よく使う値 */
  336.     width  = ( fr.rdwx - fr.lupx + 1 );
  337.     height = ( fr.rdwy - fr.lupy + 1 );
  338.     
  339.     if( (mem=PI_MALLOC( (width+1)*2  ))==NULL )
  340.         return PI_ERROR_NO_MEMORY;
  341.     buf[0] = mem;
  342.     buf[1] = mem+width+1;
  343.     
  344.     EGB_writePage( EgbPtr, PI_PAGE );
  345.     EGB_viewport ( EgbPtr, &fr );
  346.     EGB_writeMode( EgbPtr, EGB_PSET );
  347.     EGB_paintMode( EgbPtr, 0x02 );
  348.     
  349.     if( pi_imge->pix <= 8 )    /* 16/256モード */
  350.     {
  351.         getPalette( (pi_imge->pix < 8 ? 16:256 ) );
  352.         setPalette( pmode );
  353.         
  354.         EGB_color(EgbPtr, EGB_FORECOL, C_GRAY );
  355.         
  356.         if( pi_imge->pix == 4 )
  357.             mk_Relief( &fr, depth, pmode,
  358.                 getBuf, getPix16, putPix16, palcmp );
  359.         else
  360.             mk_Relief( &fr, depth, pmode,
  361.                 getBuf, getPix256,putPix256,palcmp );
  362.         
  363.     }
  364.     else    /* 32K色モード */
  365.     {    
  366.         EGB_color(EgbPtr, EGB_FORECOL, GRAY32K );
  367.         if( pmode==1 )
  368.         {
  369.             EGB_pastel     ( EgbPtr, PASTEL_LEVEL );
  370.             EGB_writeMode( EgbPtr, EGB_PASTEL );
  371.         }
  372.         
  373.         mk_Relief( &fr, depth, (pmode>1 ? 1:0 ),
  374.                 getBuf32K, getPix32k, putPix32k, rgbcmp );
  375.     }
  376.     
  377. #ifdef DEBUG
  378.     printf("clock = %d", clock()-t );
  379. #endif
  380.     
  381.     PI_FREE( mem );
  382.     
  383.     return NOERR;
  384. }
  385.  
  386. /*
  387.     768*512,512*512(32k)
  388.             V1.0    getPix    putPix        grayLineからputPixを呼ぶ
  389.     16色    1131    390        363            545
  390.     256                        290
  391.     32k                        240
  392.     
  393.     アルゴリズム変更        +10
  394.     
  395. */
  396.